package com.czkt.springboot_mybatis_test.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.czkt.springboot_mybatis_test.entity.Page;
import com.czkt.springboot_mybatis_test.entity.SysRole;
import com.czkt.springboot_mybatis_test.entity.vo.StorageProduct;
import com.czkt.springboot_mybatis_test.service.IStorageService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2024-06-18
 */
@Controller
@RequestMapping("/springboot_mybatis_test/storage")
public class StorageController {
    @Resource
    IStorageService storageService;

    @RequestMapping("/crm/storage/list")
    public String storageShow(String prodName, String stkWarehouse, Integer pageIndex, Model model){
        if(pageIndex == null){
            pageIndex = 1;
        }
        Page page = new Page();
        if(prodName == null){
            prodName = "";
        }
        if(stkWarehouse == null){
            stkWarehouse = "";
        }
        Integer count = storageService.StorageAndProductCount(stkWarehouse, prodName);
        if(count == 0){
            count = 1;
        }
        page.setTotalCount(count);
        if(pageIndex < 1){
            pageIndex = 1;
        }
        if(pageIndex > page.getTotalPageCount()){
            pageIndex = page.getTotalPageCount();
        }
        int index = (pageIndex - 1) * page.getPageSize();
        page.setPageNo(pageIndex);

        List<StorageProduct> storageProducts = storageService.selectStorageAndProduct(stkWarehouse,prodName,index,page.getPageSize());
        page.setStorageProducts(storageProducts);
        model.addAttribute("storagePage",page);
        model.addAttribute("prodName",prodName);
        model.addAttribute("stkWarehouse",stkWarehouse);
        return "/storage/list";
    }
}
